package com.xyzwps.benchmarks.json;

import com.google.gson.*;

import java.util.Date;

public class GsonUtils implements JsonUtils {

    private static final Gson gson = new GsonBuilder()
            .registerTypeAdapter(Date.class, (JsonSerializer<Date>) (src, typeOfSrc, context) -> new JsonPrimitive(src.getTime()))
            .registerTypeAdapter(Date.class, (JsonDeserializer<Date>) (json, typeOfT, context) -> new Date(json.getAsLong()))
            .create();

    @Override
    public String stringify(Object obj) {
        return gson.toJson(obj);
    }

    @Override
    public <T> T parse(String s, Class<T> c) {
        return gson.fromJson(s, c);
    }

    public static final GsonUtils INSTANCE = new GsonUtils();
}
